<?php
class Merchant_Service_RmaProccess
{
	protected $_order = null;	 
	protected $_orderCode = null;
	protected $_plan = null;
	protected $_procCnt  = null;
	protected $_products = null;
	protected $_customerId = null;
	protected $_customerInfo = null;
	protected $_error;
	public static $returnResult = array(
        0 => 'destroy',	
        1 => 'restore'
    );
    
	public function __construct(){			
		$this->setCustomer();
	}
	
	private function setCustomer(){	
		if(!isset($this->_customerId)){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$this->_customerId = $customerInfo['customer_id'];
			$this->_customerInfo = $customerInfo;
		}
	
	}
	
	
	/**
	 * 创建一条新的 rma 信息
	 * 参数 $productArr: 一维数组,索引为产品id，值为 数量
	 */
	public function create($productArr,$row){
		if($this->_customerInfo['return_package_proccess'] == 0){
			return array('ask'=>0,'receiving_code'=>'','msg'=>'用户处理方式为销毁');
		}
		if(!$row['ref_id']){
			return array('ask'=>0,'receiving_code'=>'','msg'=>'Please fill the Reference No');
		}elseif (!$this->checkReferenceNo($row['ref_id'])){
			return array('ask'=>0,'receiving_code'=>'','msg'=>'Reference No. existed');
		}
		
		$code = $this->createCode();	// 返回一个新的相应rma code 
		$code && $row['receiving_code'] = $code;
		$Obj = new Merchant_Model_Receiving();
		$Obj->getAdapter()->beginTransaction();
		try{
			$lastInsertId = $Obj->add($row);
			$this->createAsnItem($productArr,$code);
//			$this->createRmaLog($row);	创建日志
			$Obj->getAdapter()->commit();
		}catch (Exception $e) {
			echo $e->getTraceAsString();
			echo $e->getMessage();
			$Obj->getAdapter()->rollBack();
			return array('ask'=>0,'receiving_code'=>'','msg'=>'Create failed');
		}
		$arr =  array('ask'=>'Success','receiving_code'=>$code,'msg'=>'Create Success');
		return $arr;
	}
	
	public function createAsnItem($productArr,$code){
		if(!$productArr || !$code){
			throw new Exception('productId or receiving_code error.');
		}
		$cdate = date('Y-m-d H:i:s');
		$itemIdArr = array();
		foreach ($productArr as $key =>$val ){
			$itemArr = array();
			$itemArr['receiving_code'] = $code;
			$itemArr['product_id'] = $key;
			$itemArr['order_qty'] = $val;
			$itemArr['created'] = $cdate;
			$obj = new Merchant_Service_ReceivingItem;
			$itemId = $obj->add($itemArr);
		}
	}
	
	/**
	 * 更新指定的 Rma 信息
	 * 参数 $productArr: 一维数组,索引为产品id，值为 数量
	 */
	public function update($productArr,$row,$value,$field){
		if(isset($row['ref_id'])){
			unset($row['ref_id']);
		}
		$Obj = new Merchant_Model_Receiving();
		$Obj->getAdapter()->beginTransaction();
		try{
			$updateId = $Obj->update($row,$value,$field);
			$info = $Obj->getByValue($value,$field);
			$res = Merchant_Service_ReceivingItem::delete($info['receiving_code'],'receiving_code');
			$this->createAsnItem($productArr,$info['receiving_code']);
//			$this->createRmaLog($row);	创建日志
			$Obj->getAdapter()->commit();
		}catch (Exception $e) {
			echo $e->getTraceAsString();
			echo $e->getMessage();
			$Obj->getAdapter()->rollBack();
			return array('ask'=>0,'msg'=>'Edit asn failed');
		}
		
		return array('ask'=>'Success','receiving_code'=>$info['receiving_code'],'msg'=>'Success');
	}
	
	/**
	 * 	检查 receiving 表 ref_id 字段值(客户参考号)的唯一性
	 *  $reference_no已存在返回 false;不存在返回 true; 
	 */
	public function checkReferenceNo($reference_no){		
		$row = Merchant_Service_Receiving::getByValue($reference_no,'ref_id');
		if($row){
			return false;
		}else {
			return true;
		}		
	}
	
	/**
	 * 	针对指定用户，生成一个新的相应 code 
	 */
	public function createCode(){
		$lastCode = $ASNNo = $tmp = '';
		$obj = new Merchant_Service_Receiving();
		// 退货
		$lastCode = $obj->getCurdateLastReturnCode($this->_customerId);			
		$ASNNo = 'M'.$this->_customerId.date('ymd');
		if($lastCode){
			$tmp= (int) substr($lastCode,-3,3);				
			$tmp= str_pad(($tmp+1),3,'0',STR_PAD_LEFT);
		}else{
			$tmp ='001';
		}
		$ASNNo .= $tmp;		
		return $ASNNo;
	}
	
	/**
	 * 	创建日志
	 */
	public function createRmaLog($row){
		$res = Merchant_Service_ReceivingLog::add($row);		
	}
	 
	 /*
	 *验证
	 */
	 
	 
}
?>